// ignore_for_file: deprecated_member_use

import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_widget/custom/custom_appbar.dart';
import 'package:flutter_widget/custom/custom_button.dart';

// ignore: must_be_immutable
class EasyrefreshPage extends StatefulWidget {
  String name;
  EasyrefreshPage({Key? key, required this.name}) : super(key: key);

  @override
  _EasyrefreshPageState createState() => _EasyrefreshPageState();
}

class _EasyrefreshPageState extends State<EasyrefreshPage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: CustomAppbar(widget.name, Theme.of(context).primaryColor),
        body: _Example());
  }
}

class _Example extends StatefulWidget {
  @override
  _ExampleState createState() {
    return _ExampleState();
  }
}

class _ExampleState extends State<_Example> {
  late EasyRefreshController _controller;

  // 条目总数
  int _count = 20;

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: EasyRefresh.custom(
        enableControlFinishRefresh: false,
        enableControlFinishLoad: true,
        controller: _controller,
        header: ClassicalHeader(
          refreshText: '预加载',
          refreshReadyText: '准备加载',
          refreshingText: "加载中",
          refreshedText: "加载完成",
          noMoreText: "没有了",
          alignment: Alignment.center,
          // bgColor: Colors.transparent,
          textColor: Colors.blue,
          // infoColor :Colors.teal,
          showInfo: false,
        ),
        footer: ClassicalFooter(
          loadingText: '加载中',
          loadedText: '加载完成',
          noMoreText: '没有了',
          alignment: Alignment.center,
          // bgColor: Colors.transparent,
          textColor: Colors.blue,
          // infoColor :Colors.teal,
          showInfo: false,
        ),
        onRefresh: () async {
          await Future.delayed(Duration(seconds: 2), () {
            print('onRefresh 上拉加载');
            setState(() {
              _count = 20;
            });
            _controller.resetLoadState();
          });
        },
        onLoad: () async {
          await Future.delayed(Duration(seconds: 2), () {
            print('onLoad 下拉刷新');
            setState(() {
              _count += 10;
            });
            _controller.finishLoad(noMore: _count >= 40);
          });
        },
        slivers: <Widget>[
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (context, index) {
                return Container(
                  width: 60.0,
                  height: 60.0,
                  child: Center(
                    child: Text('$index'),
                  ),
                  color: index % 2 == 0 ? Colors.grey[300] : Colors.transparent,
                );
              },
              childCount: _count,
            ),
          ),
        ],
      ),
      persistentFooterButtons: <Widget>[
        CustomButton(
          onPressed: () {
            _controller.callRefresh();
          },
          width: 100,
          content: Text(
            "Refresh",
            style: TextStyle(color: Colors.white),
          ),
        ),
        CustomButton(
          width: 100,
          onPressed: () {
            _controller.callLoad();
          },
          content: Text(
            "Load more",
            style: TextStyle(color: Colors.white),
          ),
        ),
      ],
    );
  }
}
